package array;

public class Demo8 {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8};
        reverseArray(arr);
    }

    //数组元素的反转并打印
    public static void reverseArray(int[] arr) {
        for (int i = 0; i < arr.length / 2; i++) {
            //arr[0]和arr[arr.length - 1 - 0]交换
            //arr[1]和arr[arr.length - 1 - 1]交换
            //arr[2]和arr[arr.length - 1 - 2]交换
            //....

            //交换元素
            int temp = arr[i];
            arr[i] = arr[arr.length - 1 - i];
            arr[arr.length - 1 - i] = temp;

        }

        for (int i = 0; i < arr.length; i++) {
            System.out.println("arr[" + i + "]" + "=" + arr[i]);
        }
    }

}
